package com.leetcode.partition5;

import java.util.ArrayList;
import java.util.List;

/**
 * @author `RKC`
 * @date 2021/8/14 10:14
 */
public class LC491递增子序列 {

    public static List<List<Integer>> findSubsequences(int[] nums) {
        List<List<Integer>> answer = new ArrayList<>();
        backtracking(answer, new ArrayList<>(), 0, nums);
        return answer;
    }

    public static void main(String[] args) {
//        int[] nums = {4, 6, 7, 7};
        int[] nums = {4, 4, 3, 2, 1};
        System.out.println(findSubsequences(nums));
    }

    private static void backtracking(List<List<Integer>> answer, List<Integer> path, int startIndex, final int[] nums) {
        if (path.size() >= 2) answer.add(new ArrayList<>(path));
        //记录同层元素的使用情况
        boolean[] used = new boolean[201];
        for (int i = startIndex; i < nums.length; i++) {
            //同一层的元素不能重复使用
            if (path.size() >= 1 && path.get(path.size() - 1) > nums[i] || used[nums[i] + 100]) continue;
            path.add(nums[i]);
            used[nums[i] + 100] = true;         //记录这个元素在本层已经使用过，本层后面不能再使用了
            backtracking(answer, path, i + 1, nums);
            path.remove(path.size() - 1);
        }
    }
}
